<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			//输入：tokens = ["2","1","+","3","*"]
			//输出：9
			//解释：该算式转化为常见的中缀算术表达式为：((2 + 1) * 3) = 9
			/**
			 * @param {string[]} tokens
			 * @return {number}
			 */
			var evalRPN = function(tokens) {
				const calculate = (num1, num2, operator) => {
					switch (operator) {
						case "+":
							return num1 + num2;
						case "-":
							return num1 - num2;
						case "*":
							return num1 * num2;
						case "/":
							return parseInt(num1 / num2, 10);
						default:
							return 0;
					}
				};
				let stack = [];
				for (let v of tokens) {
					switch (v) {
						case "+":
						case "-":
						case "*":
						case "/":
							let num1 = stack.pop();
							let num2 = stack.pop();
							// 注意下面调用calculate函数时第一个参数是num2而非num1
							stack.push(calculate(num2, num1, v));
							break;
						default:
							stack.push(parseInt(v, 10));
					}
				}
				return stack.pop();
			}
			// console.log(evalRPN(["2","1","+","3","*"]));
			// console.log(evalRPN(["4","13","5","/","+"]));
			console.log(evalRPN(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]));
		</script>
	</body>
</html>
